{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e7de45bd",
   "metadata": {},
   "source": [
    "Build and run Jupyter Notebook server from `/fuzzylite` as:\n",
    "\n",
    "```bash\n",
    "make jupyter\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f6254062",
   "metadata": {},
   "outputs": [],
   "source": [
    "#pragma cling add_library_path(\"/usr/local/lib\")\n",
    "#pragma cling load(\"fuzzylite-debug\")\n",
    "\n",
    "// Use this if built in release mode\n",
    "// #pragma cling load(\"fuzzylite\")\n",
    "\n",
    "#include <fuzzylite/Headers.h>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d513faa7",
   "metadata": {},
   "outputs": [],
   "source": [
    "fl::Engine* engine = fl::FllImporter().fromFile(\"../mamdani/ObstacleAvoidance.fll\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fc991592",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Engine: ObstacleAvoidance\n",
      "InputVariable: obstacle\n",
      "  enabled: true\n",
      "  range: 0.000 1.000\n",
      "  lock-range: false\n",
      "  term: left Ramp 1.000 0.000\n",
      "  term: right Ramp 0.000 1.000\n",
      "OutputVariable: mSteer\n",
      "  enabled: true\n",
      "  range: 0.000 1.000\n",
      "  lock-range: false\n",
      "  aggregation: Maximum\n",
      "  defuzzifier: Centroid 100\n",
      "  default: nan\n",
      "  lock-previous: false\n",
      "  term: left Ramp 1.000 0.000\n",
      "  term: right Ramp 0.000 1.000\n",
      "RuleBlock: mamdani\n",
      "  enabled: true\n",
      "  conjunction: none\n",
      "  disjunction: none\n",
      "  implication: AlgebraicProduct\n",
      "  activation: General\n",
      "  rule: if obstacle is left then mSteer is right\n",
      "  rule: if obstacle is right then mSteer is left"
     ]
    }
   ],
   "source": [
    "std::cout << engine->toString();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e12835eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "std::string status;\n",
    "if (not engine->isReady(&status))\n",
    "    throw fl::Exception(\"[engine error] engine is not ready:\\n\" + status, FL_AT);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c9dc84c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "obstacle.input = 0.000 => steer.output = 0.667\n",
      "obstacle.input = 0.100 => steer.output = 0.660\n",
      "obstacle.input = 0.200 => steer.output = 0.638\n",
      "obstacle.input = 0.300 => steer.output = 0.602\n",
      "obstacle.input = 0.400 => steer.output = 0.554\n",
      "obstacle.input = 0.500 => steer.output = 0.500\n",
      "obstacle.input = 0.600 => steer.output = 0.446\n",
      "obstacle.input = 0.700 => steer.output = 0.398\n",
      "obstacle.input = 0.800 => steer.output = 0.362\n",
      "obstacle.input = 0.900 => steer.output = 0.340\n",
      "obstacle.input = 1.000 => steer.output = 0.333\n"
     ]
    }
   ],
   "source": [
    "fl::InputVariable* obstacle = engine->getInputVariable(\"obstacle\");\n",
    "fl::OutputVariable* steer = engine->getOutputVariable(\"mSteer\");\n",
    "\n",
    "int resolution = 10;\n",
    "for (int i = 0; i <= resolution; ++i){\n",
    "    float dx = obstacle->range() / resolution;\n",
    "    fl::scalar location = obstacle->getMinimum() + i * dx;\n",
    "    obstacle->setValue(location);\n",
    "    engine->process();\n",
    "    std::cout << \"obstacle.input = \" << fl::Op::str(location) << \n",
    "        \" => \" << \"steer.output = \" << fl::Op::str(steer->getValue()) << std::endl;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f97954ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "delete engine;"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++11",
   "language": "C++11",
   "name": "xcpp11"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
